<?php

namespace app\mobile\controller\consult\offline;

use app\admin\service\UserService;
use app\BaseController;
use app\model\ConsultTimeModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 线下预约咨询师信息
     * @return bool|string
     */
    public function consultInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require'
            ]);
            $UserModel = new UserModel();
            $info = $UserModel->with([
                'userTag' => function ($query) {
                    $query->with(['tag']);
                },
                'unitName'
            ])->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info = $info->toArray();
            $ConsultTimeModel = new ConsultTimeModel();
            $time_department_id = $ConsultTimeModel->where('consult_id','=',$param['id'])->column('department_id');
            if (!empty($time_department_id)){
                $time_department_id = array_unique($time_department_id);
                if (in_array(0, $time_department_id)) {
                    $info['responsible_department'] = '所有部门';
                }else{
                    $UserService = new UserService($this->app);
                    $NotDelDepartmentAll = $UserService->getNotDelDepartmentAffiliationAll($this->userData['unit_id']);
                    foreach ($time_department_id as $department_id){
                        if (!empty($info['responsible_department'])){
                            $info['responsible_department'] .= '、'.$NotDelDepartmentAll[$department_id]['all_name'];
                        }else{
                            $info['responsible_department'] = $NotDelDepartmentAll[$department_id]['all_name'];
                        }
                    }
                }
            }else{
                $info['responsible_department'] = '--';
            }
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}